package com.cb.kfc.controller;

import com.cb.kfc.domain.Product;
import com.cb.kfc.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;

    //会员类别添加
    @RequestMapping("/save.do")
    public String save(Product product) throws Exception {
        productService.save(product);
        return "redirect:findAll.do";
    }

    //查询全部会员类别，//只有admin才能访问
    @RequestMapping("/findAll.do")
//    @Secured("ROLE_Guest")
    public ModelAndView findAll() throws Exception {
        ModelAndView mv = new ModelAndView();
        List<Product> ps = productService.findAll();
        mv.addObject("productList",ps);
        mv.setViewName("product-list");
        return mv;
    }
    /**
     * 删除
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete.do")
    public String delete(String[] ids) throws Exception{
        Integer  integer = productService.deleteProductById(ids);
        return "redirect:findAll.do";
    }

    //查询单个会员类别
    @RequestMapping("/findOne.do")
    public ModelAndView findOne(String id) throws Exception {
        ModelAndView mv = new ModelAndView();
        Product product = productService.findOne(id);
        mv.addObject("product",product);
        mv.setViewName("product-update");
        return mv;
    }

    //会员类别更新
    @RequestMapping("/update.do")
    public String Update(Product product) throws Exception {
        productService.update(product);
        return "redirect:findAll.do";

    }

    @RequestMapping("/product-add.do")
//    @Secured("ROLE_Guest")
    public ModelAndView productAdd() throws Exception {
        ModelAndView mv = new ModelAndView();

        mv.setViewName("product-add");
        return mv;
    }
}
